package es.ucm.disia.rcei.example.voice2;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import es.ucm.disia.rcei.RCEICommand;

public class VoiceGUI extends JFrame 
{
	private JTextArea codeTextArea; 
	private JButton loadButton;
	private JPanel mainPanel, secondPanel;
	
	private String bufferText;
	private OrderDispatcher speechRecognizer; 
	
	/**
	 * RCEIWizard constructor
	 * */
  	public VoiceGUI()
  	{  		
  		super();  		
  		speechRecognizer = new OrderDispatcher("rce" + File.separator+ "nwn1.rcei");
  		this.bufferText="";
  		try
    	{		       
    		   UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");   		
		       SwingUtilities.updateComponentTreeUI(this);		       
		       this.setTitle("GUI Vocal Wizard");
		       this.getContentPane().setBackground(new Color(169, 169, 169));
		       this.setSize(new Dimension(350, 200));
		       this.setPreferredSize(new Dimension(350, 200));
		       //this.setResizable(false);
		       initButtons();		       	       		      		 
		       initActions();		 
		       
		       this.getContentPane().add(mainPanel);
		       //this.getContentPane().add(secondPanel);
		}
		catch(Exception e) {e.printStackTrace();}
  	}
  	
  	/**
	 * This method init all buttons of the Panel
	 * */
	private void initButtons()
  	{
		mainPanel= new JPanel();
		secondPanel = new JPanel();
		loadButton = new JButton();
		//loadButton.setBounds(259, 515, 130, 23);		   
		loadButton.setText("Recognize order");
	   		   
		//Text of XML format RCEI message
		codeTextArea = new JTextArea();
		//	codeTextArea.setEditable(false);
		codeTextArea.setPreferredSize(new Dimension(350, 100));
		codeTextArea.setFont(new Font("Courier New", Font.PLAIN, 11));
		
		mainPanel.add(loadButton);
		mainPanel.add(codeTextArea);
		
  	}
  	
	private void initActions()
  	{
		loadButton.addActionListener (new ActionListener ()
		{
			  public void actionPerformed (ActionEvent evt)
			  {				  	
					RCEICommand cmd=new RCEICommand("","");
					String speech, subject, process;	
					//speechRecognizer.startup();
					speechRecognizer.recognizeProcess() ;
				    speech = speechRecognizer.getOrderRecognized();
				  	System.out.println(speech);
					
				  	if (speech.contains(" "))
				  	{
						subject= speech.substring(0,speech.indexOf(" "));
						process= speech.substring(speech.indexOf(" "),speech.length());
						cmd = speechRecognizer.generateCommandAction(subject, process);
					}
					else if (speech!="")
					{
						subject=speech;
						cmd = speechRecognizer.generateCommandAction(subject, "");				
					}
				  	//speechRecognizer.shutdown();
					
				  	
				  	codeTextArea.setText("You said:" +speech+"\n");
				  	if (cmd !=null)
				  	{
				  		codeTextArea.append("Sending order:" + cmd.toString());
				  		speechRecognizer.sendCmd(cmd);
				  	}
			  }
		});
  	}
  	
	/** 
	 * Overridden so we can exit when window is closed
	 * */
  	protected void processWindowEvent(WindowEvent e)
  	{
  		super.processWindowEvent(e);
   		if (e.getID() == WindowEvent.WINDOW_CLOSING)
   		{
    		System.exit(0);
    	}
  	}
  	
	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		System.out.println("Starting RCEI Voice GUI...");
		VoiceGUI gui = new VoiceGUI();
        gui.setVisible(true);
        gui.setEnabled(true);  

	}

}
